﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringVariables
{
    public static class Extensions
    {

        public static IDictionary<string, string> CreateNewVariables(this IEnumerable<VariableDefinition> defs)
        {
            var vars = new Dictionary<string, string>();
            foreach (var def in defs)
            {
                vars.Add(def.Name, def.DefaultValue);
            }
            return vars;
        }

        public static IEnumerable<string> Validate(this IEnumerable<VariableDefinition> defs, IDictionary<string, string> variables)
        {
            foreach (var def in defs)
            {
                if (variables.ContainsKey(def.Name))
                {
                    if (!def.Type.IsValid(variables[def.Name]))
                        yield return def.Name;
                }
                else
                {
                    if (!def.AllowNull && string.IsNullOrEmpty(def.DefaultValue))
                        yield return def.Name;
                }
            }
        }
    }
}
